class SongsController < ApplicationController
  #filter_access_to :all
  layout Proc.new { |controller| controller.request.xhr? ? 'slim' : 'main' }


  # GET /songs/id
  # GET /songs/id.xml
  def show
    @song = Song.find(params[:id])
	@lyrics = Lyrics.new(@song.artists.first, @song.title)
	
    session[:edit_mode] = false
    store_location

    respond_to do |format|
      format.html
      format.xml { render :xml => @song }
    end
  end

  # GET /songs/new
  # GET /songs/new.xml
  def new
	
    @song = Song.new

    if params[:title]
      @song.title = params[:title]
    end
  end
 
  #
  #
  def create
    @song = Song.new(params[:song])
    
    #Add artist
    if @song.edition
      artist = @song.edition.release.artists.first
      @song.artists << artist
    elsif params[:artist]
      artist = Artist.find(params[:artist][:artist_id])
      @song.artists << artist
    end

    if @song.save
      flash[:notice] = "Song Added"
      redirect_to song_path(@song)
    else
      render :new
    end 
  end
 
  #
  #
  def edit
    @song = Song.find(params[:id])
	@lyrics = Lyrics.new(@song.artists.first, @song.title)
    session[:edit_mode] = true
    store_location
  end

  #
  #
  def update
    @song = Song.find(params[:id])

    if @song.update_attributes(params[:song])
      flash[:notice] = "Updated successfully"
      redirect_to edit_song_path(@song)
    else
      render :edit
    end
  end

end
